﻿<?php
ini_set('date.timezone','Asia/Shanghai');
include 'simple_html_dom.php';
include 'config_catchbaidu.php';

define('APPS_NUM',2405);
define('GAMES_NUM',977);

define('DBPAS',"coolchuan2012");
//define('DBPAS',"123456");

$link = mysql_connect("localhost","root",DBPAS); 
mysql_select_db("catchappdataall", $link);
mysql_query('SET NAMES UTF8');


define('ONCECATCHCOUNT',30);
define('ONCECOMMENTCATCHCOUNT',50);

define('WEBURLHEADER','http://as.baidu.com/a/item?docid=');

define('LOGPATHHEADER','/var/www/catchjiankongdata/');
//define('LOGPATHHEADER','');

define('LOGFORSUCCESS',1);

define('WAPURLHEADERBAIDU','http://as.baidu.com/a/item?docid=');
define('WAPURLFOOTERBAIDU','&pre=web_am_software&pos=software_1012_0&f=suite_3%40software_1012_0&suite=3&bdtype=soft&appname=soft&version=2.2');
define('WEBURLHEADERBAIDU','http://as.baidu.com/a/item?docid=');


//define('FROMID','testiD');
//define('TOKEN','test');

define('FROMID','1001602a');
define('TOKEN','tianjiao');


getpnanid();



function getpnanid()
{
//	$url = "http://m.baidu.com/api?action=detail&from=testiD&token=test&type=app";
//	$poststring = '[{"package":"com.tencent.qqpinyin","versioncode":"1127","signmd5":"345345"},{"package":"com.tencent.qq","versioncode":"61","signmd5":"23443564"}]';
//    echo (post($url,$poststring));
    
	$app_exit_infos = get_exit_appinfo_fromdb();

	foreach ($app_exit_infos as $app_exit_info)
	{
		$createtime = date("Y-m-d H:i:s");
		$id = $app_exit_info['id'];
		writelog(1,$id,$createtime,1);
		$app_id = $app_exit_info['app_id'];
		$packagename = $app_exit_info['packageName'];
		$url = "http://m.baidu.com/api?action=search&from=".FROMID."&token=".TOKEN."&type=app&package=$packagename";
	    
	    writelog(1,$id,$createtime,11);
	    $result_arr = getcurrentappid($url);
	    writelog(1,$id,$createtime,4);
	    
	    if(($result_arr['currentid'] == "") && ($result_arr['packagename'] == ""))
	    {
	    	writedatanotexit(1,$id,$packagename,1);
	    }
	    else 
	    {
	    	writedataexit(1,$id,$packagename,1);
	    }
	    
	    continue ;
//	    if(($app_id < $result_arr['currentid']) && ($result_arr['currentid'] != "") && ($app_id != "") && ($packagename == $result_arr['packagename']))
//	    {
//	    	writelog(1,$id,$createtime,5);
//	    	update_appscore_todb($id,$result_arr['currentid'],$app_id);
//	    	writelog(1,$id,$createtime,6);
//	    }
//	    else 
//	    {
//	    	writelog(1,$id,$createtime,7);
//	    }
	}
	
}



function writelog($rs,$id,$time,$flag)
{
	$time = date("Y-m-d H:i:s");
	$filename_update = LOGPATHHEADER.'logs/baiduupdatepnid_chenckisexit.log';
	
	try
	{
		//记录错误，二次抓取用
		$info_str = $id."+".$flag."--".$time."\r\n";
		$fh = fopen($filename_update, "a");
		fwrite($fh, $info_str);
		fclose($fh);
	}
	catch(Exception $e)
	{
		
	}
}


function writedatanotexit($rs,$id,$packagename,$flag)
{
	$time = date("Y-m-d H:i:s");
	$filename_update = LOGPATHHEADER.'logs/baiduupdatepnid_datanotexit.log';
	
	try
	{
		//记录错误，二次抓取用
		$info_str = $id."+".$flag."--".$packagename."=".$time."\r\n";
		$fh = fopen($filename_update, "a");
		fwrite($fh, $info_str);
		fclose($fh);
	}
	catch(Exception $e)
	{
		
	}
}
function writedataexit($rs,$id,$packagename,$flag)
{
	$time = date("Y-m-d H:i:s");
	$filename_update = LOGPATHHEADER.'logs/baiduupdatepnid_dataexit.log';
	
	try
	{
		//记录错误，二次抓取用
		$info_str = $id."+".$flag."--".$packagename."=".$time."\r\n";
		$fh = fopen($filename_update, "a");
		fwrite($fh, $info_str);
		fclose($fh);
	}
	catch(Exception $e)
	{
		
	}
}



function getcurrentappid($xml_url)
{
	$createtime = date("Y-m-d H:i:s");
	$result_arr = array();
	$result_arr['packagename'] = "1";
	$result_arr['currentid'] = "1";
	writelog(1,111,$createtime,2);
	$reader = new XMLReader();
	$reader->open($xml_url, 'utf-8');
	writelog(1,1111,$createtime,3);
	while ($reader->read()) 
	{
		if($result_arr['packagename'] == "1" && $result_arr['currentid'] == "1")
		{
			$result_arr['packagename'] = "";
			$result_arr['currentid'] = "";
		}
		if ($reader->name == "package" && $reader->nodeType == XMLReader::ELEMENT) 
		{
			while($reader->read() && $reader->name != "package") 
			{
				$name = $reader->name;
				$result_arr['packagename'] = $reader->value;
				break;
			}
		}
		if ($reader->name == "docid" && $reader->nodeType == XMLReader::ELEMENT) 
		{
			while($reader->read() && $reader->name != "docid") 
			{
				$name = $reader->name;
				$result_arr['currentid'] = $reader->value;
				break;
			}
		}
		if(($result_arr['packagename'] != "") && ($result_arr['currentid'] != ""))
		{
			break;
		}
	}
	$reader->close();
	return $result_arr;
}




function post($url, $post = null)  
{  
    $context = array();  
  
    if (is_array($post))  
    {  
        ksort($post);  
  
        $context['http'] = array  
        (  
            'method' => 'post',  
            'header'=>'content-type: application/x-www-form-urlencoded'."rn". 
                      'user-agent : jimmy'."rn". 
                      'content-length: '.strlen($post_string)+8, 
            'content' => http_build_query($post, '', '&'),  
        );  
    }  
  
    return file_get_contents($url, false, stream_context_create($context));  
}  


function get_exit_appinfo_fromdb()
{
	$app_exit_info = array();
	$link = mysql_connect("localhost","root",DBPAS); 
	$createtime = strtotime(date("Y-m-d H:i:s"));
	
	$sql = 'select id, app_id, packageName from jk_apps_info_baidu order by FTime asc'; 
	$rs = mysql_query($sql, $link);
	
	$app_exit_info = array();
  	while($row=mysql_fetch_array($rs))        
  	{
  		$app_exit_info_temp = array();
  		$app_exit_info_temp['id'] = $row['id'];
  		$app_exit_info_temp['app_id'] = $row['app_id'];
	  	$app_exit_info_temp['packageName'] = $row['packageName'];
	  	$app_exit_info[] = $app_exit_info_temp;
  	}
  	
//  	$appid_arr = array();
//  	foreach ($app_exit_info_result as $app_exit_info_result_temp)
//  	{
//  		if(!in_array($app_exit_info_result_temp['id'],$appid_arr))
//  		{
//  			$appid_arr[] = $app_exit_info_result_temp['id'];
//  			$app_exit_info[] = $app_exit_info_result_temp;
//  		}
//  		
//  	}

	return $app_exit_info;
}


//监控信息
function update_appscore_todb($id,$app_id,$app_id_old)
{
	if(($id == 0) || ($app_id == 0))
	{
		return ;
	}
	$link = mysql_connect("localhost","root",DBPAS); 
	$url = WEBURLHEADER.$app_id;
	$sql = "update jk_apps_info_baidu set app_id = $app_id,url = '".$url."' where id = $id and app_id = $app_id_old ";
	
	$rs = mysql_query($sql, $link);
	
	$filename_success = LOGPATHHEADER.'logs/baiduupdatepnid_success.log';
	$filename_fail = LOGPATHHEADER.'logs/baiduupdatepnid_fail.log';
	$filename_db_fail = LOGPATHHEADER.'logs/baiduupdatepnid_db_fail.log';

	try
	{
		if(!$rs)
		{
			//记录错误，二次抓取用
			$info_str = $sql."Valid result!\r\n";
			$fh = fopen($filename_db_fail, "a");
			fwrite($fh, $info_str);
			fclose($fh);
			echo $info_str;
		}
		else
		{
			if(LOGFORSUCCESS)
			{
				$info_str = $id."success!\r\n";
				$filename = $filename_success;
				$fh = fopen($filename, "a");
				fwrite($fh, $info_str);
				fclose($fh);
			}
		}
	}
	catch(Exception $e)
	{
		
	}
}

?>